package sapling.service;

import io.goeasy.GoEasy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sapling.dao.*;
import sapling.model.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by mac on 17/7/14.
 */
@Service
@Transactional
public class TreeService {
    @Autowired
    TreeDao treeDao;
    @Autowired
    TreeWellDao treeWellDao;
    @Autowired
    LandMassDao landMassDao;
    @Autowired
    MarkorderDao markorderDao;
    @Autowired
    OutboundOrderDao outboundOrderDao;
    @Autowired
    AdminDao adminDao;
    @Autowired
    PutOutService putOutService;


    /**
     * 确认死苗
     * @param salesState
     * @param markorder_id
     */
    public void dieConfirm(int salesState,Long markorder_id){
        Markorder markorder=markorderDao.findOne(markorder_id);
        if(Objects.nonNull(markorder)){
            List<Tree> trees=markorder.getTreesList();
            for (Tree value:trees) {
                if(Objects.nonNull(value.getTree_well_id())){
                    TreeWell treeWell=treeWellDao.findOne(value.getTree_well_id());
                    if(Objects.nonNull(treeWell)){
                        treeWell.setTree_id(0);
                        treeWell.setUseState(0);
                        treeWellDao.save(treeWell);
                    }
                }
                value.setSalesState(salesState);
                value.setLandmassid(0);
                value.setTree_well_id(0);
                treeDao.save(value);
            }
            markorder.setSalestate(salesState);
            markorderDao.save(markorder);
        }
    }

    /**
     * app出库确认
     */
    public void appoutwarehouse(int salesState,Long treeid,BigDecimal price,String direction){
        Tree tree=treeDao.findOne(treeid);
        if(Objects.nonNull(tree)&&Objects.nonNull(tree.getOutboundOrder())){
            OutboundOrder  outboundOrder=outboundOrderDao.findOne(tree.getOutboundOrder().getOutboundOrder_id());
            TreeWell treeWell=treeWellDao.findbytree(treeid);
            if(Objects.nonNull(treeWell)){
                treeWell.setUseState(0);
                treeWell.setTree_id(0);
                treeWellDao.save(treeWell);
            }
            tree.setSalesState(salesState);
            tree.setTree_well_id(0);
            tree.setLandmassid(0);
            if(direction!=null&&direction.length()>0){
                tree.setDirection(direction);
            }
            tree.setOutWarehouseTime(new Date());
            treeDao.save(tree);
            outboundOrder.setTransactionAmount(price);
            outboundOrder.setStatus(salesState);
            outboundOrder.setDirection(direction);
            outboundOrderDao.save(outboundOrder);
        }
    }


    /**
     * 出库确认
     */
    public void outwarehouse(int salesState,Long markorder_id,BigDecimal price,String direction){
        OutboundOrder outboundOrder=outboundOrderDao.findOne(markorder_id);
        if(Objects.nonNull(outboundOrder)){
            List<Tree> trees=outboundOrder.getTrees();
            for (Tree value:trees) {
                TreeWell treeWell=treeWellDao.findOne(value.getTree_well_id());
                if(Objects.nonNull(treeWell)){
                    treeWell.setUseState(0);
                    treeWell.setTree_id(0);
                    treeWellDao.save(treeWell);
                }
                value.setSalesState(salesState);
                value.setTree_well_id(0);
                value.setLandmassid(0);
                value.setDirection(direction);
                value.setOutWarehouseTime(new Date());
                treeDao.save(value);
            }
            outboundOrder.setTransactionAmount(price);
            outboundOrder.setStatus(salesState);
            outboundOrder.setDirection(direction);
            outboundOrderDao.save(outboundOrder);
        }
    }


    /**
     * 确认死苗
     * @param salesState
     */
    public void appdieConfirm(int salesState,Long treeid){
        Tree tree=treeDao.findOne(treeid);
        if(Objects.nonNull(tree)&&Objects.nonNull(tree.getMarkorder())){
            Markorder  markorder=markorderDao.findOne(tree.getMarkorder().getMarkorder_id());
            List<Tree> trees=markorder.getTreesList();
            for (Tree value:trees) {
                if(Objects.nonNull(value.getTree_well_id())){
                    TreeWell treeWell=treeWellDao.findOne(value.getTree_well_id());
                    treeWell.setTree_id(0);
                    treeWell.setUseState(0);
                    treeWellDao.save(treeWell);
                }
                value.setSalesState(salesState);
                value.setLandmassid(0);
                value.setTree_well_id(0);
                treeDao.save(value);
            }
            markorder.setSalestate(salesState);
            markorderDao.save(markorder);
        }
    }
    /**
     * 确认死苗
     * @param salesState
     */
    public void appdieConfirm2(int salesState,Long treeid){
        Tree tree=treeDao.findOne(treeid);
        tree.setSalesState(salesState);
        tree.setLandmassid(0);
        if(tree.getTree_well_id()!=0){
            TreeWell treeWell=treeWellDao.findOne(tree.getTree_well_id());
            treeWell.setTree_id(0);
            treeWell.setUseState(0);
            treeWellDao.saveAndFlush(treeWell);
        }
        tree.setTree_well_id(0);
        treeDao.saveAndFlush(tree);
    }

    /**
     * 统一封装一个Tree
     * @return
     */
    public List<Tree> mark(List<Markorder> markorders){
        List<Tree> trees=new ArrayList<>();
        for(Markorder mark:markorders){
            if(Objects.nonNull(mark.getTreesList())&&mark.getTreesList().size()>0){
                Tree tree = mark.getTreesList().get(0);
                tree.setTree_id(mark.getMarkorder_id());
                tree.setNumber(mark.getQuantity());
                tree.setCreatetime(mark.getCreatetime());
                tree.setUsername(mark.getName());
                trees.add(tree);
            }
        }
        return trees;
    }

    /**
     * 出库订单封装
     * @return
     */
    public List<Tree> out() {
        List<OutboundOrder> outStatus = outboundOrderDao.findByStatus(0);
        List<Tree> treeList=new ArrayList<>();
        int number=0;
        for(OutboundOrder out:outStatus){
            if(Objects.nonNull(out.getTrees())&&out.getTrees().size()>0){
                Tree tree = out.getTrees().get(0);
                tree.setTree_id(out.getOutboundOrder_id());
                tree.setNumber(Integer.parseInt(out.getQuantity()+""));
                tree.setPrice(out.getTransactionAmount().doubleValue());
                tree.setCreatetime(out.getGmt_payment());
                tree.setUsername(out.getName());
                for (Tree value:out.getTrees()) {
                    if(Tree.TYPE_TREE_AWAIT_OUT==value.getSalesState()){
                        number++;
                    }
                }
                if(number>0){
                    treeList.add(tree);
                }
            }
        }
        return treeList;
    }

    /**
     * 消息推送
     */
    public void sandinfo(){
        int number=0;
        List<Tree> treeList= putOutService.put();
        if(Objects.nonNull(treeList)&&treeList.size()>0){
            number=treeList.size();
        }

        int number2=0;
        List<Tree> trees=treeDao.listout(Tree.TYPE_TREE_AWAIT_OUT);
        if(Objects.nonNull(trees)&&trees.size()>0){
            number2=trees.size();
        }
        int number3=0;
        List<Tree> treesdie =treeDao.listdie(5);
        if(Objects.nonNull(treesdie)&&treesdie.size()>0){
            number3=treesdie.size();
        }
//        for (Admin admin:adminDao.findAll()) {
//            for (Role value:admin.getRoleList() ) {
//                if((number+number2+number3>0)&&value.getName().equals("admin")||value.getName().equals("finance")){
//                    GoEasy goEasy = new GoEasy("BC-a5c00f8af086408888488e852b557b81");
//                    goEasy.publish(admin.getUsername(),"您有"+(number+number2+number3)+"个待处理消息！");
//                }
//            }
//        }
    }

    public List<Tree> outtree() {
        List<OutboundOrder> outStatus = outboundOrderDao.findByStatus(1);
        List<Tree> treeList=new ArrayList<>();
        for(OutboundOrder out:outStatus){
            if(Objects.nonNull(out.getTrees())&&out.getTrees().size()>0){
                Tree tree = out.getTrees().get(0);
                tree.setTree_id(out.getOutboundOrder_id());
                tree.setSerialNumber(out.getGmt_payment()+"");
                tree.setNumber(Integer.parseInt(out.getQuantity()+""));
                if(Objects.nonNull(out.getTransactionAmount())){
                    tree.setPrice(out.getTransactionAmount().doubleValue());
                }else {
                    tree.setPrice(0);
                }
                tree.setUsername(out.getName());
                treeList.add(tree);
            }
        }
        return treeList;
    }

    public void gather(Long id) {
        OutboundOrder outStatus = outboundOrderDao.findOne(id);
        if(Objects.nonNull(outStatus)){
            for (Tree tree:outStatus.getTrees()) {
                tree.setGather(true);
                treeDao.save(tree);
            }
        }
    }

}
